package com.twu.biblioteca;

import com.sun.tools.javac.util.Name;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Runner {
    LoginManager loginManager;
    Library library;
    BufferedReader br;

    public static void main(String[] args) throws IOException, UnAuthorizedUser {
        Runner runner = new Runner();
        runner.setUp();
        while (true) {
            try {
                runner.loginToLibrary();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public void loginToLibrary() throws Exception {
        int choice = showMenuAndGetUserChoice();
        switch (choice) {
            case 1:
                while (!loginManager.isUserLoggedIn())
                    getLoginInformation();
                //LinkedHashMap<Integer, String> menuOptions;
                LinkedHashMap<Integer, Action> actionList;
                while (loginManager.isUserLoggedIn()) {
                    try {
                        displayTheMenu(loginManager.currentRole);
                        actionList = loginManager.currentRole.getActions();
                        getUserChoice(actionList).execute();

                    } catch (NumberFormatException e1) {
                        System.out.println("Please...you were Supposed to enter a number");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                }
                break;
            case 2:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid Choice");
                break;

        }
    }

    private Action getUserChoice(LinkedHashMap<Integer, Action> menuOptions) throws IOException {
        Integer choice = Integer.parseInt(br.readLine());
        Action action = menuOptions.get(choice);
        return action;
    }


    public void displayTheMenu(Role role) {
        //Enumeration e = actionList.keys();
        Collection e = role.getActions().keySet();
        Iterator i = e.iterator();
        while (i.hasNext()) {
            Integer key = (Integer) i.next();
            System.out.println(key + " : " + role.getActions().get(key).printActionName());
        }
    }

    public void setUp() {
        br = new BufferedReader(new InputStreamReader(System.in));
        library = new Library();
        library.addItem(new Book("Harry Potter", "Rowling", 1990));
        library.addItem(new Video("Wizard of Oz", "Tinman", 1980, 10));
        library.registerNewCustomer(new Customer("Tina Maria", "tinamt@tw.com", "923123123", "111-2222"));
        library.registerNewCustomer(new Customer("Uppu Divya", "divya@tw.com", "29132123", "999-0000"));
        library.registerNewCustomer(new Customer("Ms. Library", "libr@tw.com", "29132123", "000-0000"));
        loginManager = new LoginManager(library);
    }

    public void getLoginInformation() throws IOException, UnAuthorizedUser {
        System.out.println("Enter username : ");
        String inputUsername = br.readLine();
        System.out.println("Enter password : ");
        String inputPassword = br.readLine();
        loginManager.userLogin(inputUsername, inputPassword);
    }

    private int showMenuAndGetUserChoice() throws IOException {
        System.out.println("1. Login");
        System.out.println("2. Exit");
        System.out.print("Enter your choice : ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        return Integer.parseInt(br.readLine());
    }
}
